﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Diagnostics;
using WMSUIMVC.Models;
using XuanKeZz.Application.HTTP;

namespace WMSUIMVC.Controllers
{
    public class UserController : Controller
    {
        private readonly IConfiguration _configuration;

        public UserController(IConfiguration configuration)
        {
            _configuration = configuration;
        }



        /// <summary>
        /// 注册
        /// </summary>
        /// <returns></returns>
        public IActionResult User()
        {
            return View();
        }

        /// <summary>
        ///登陆
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> UserDo(string model)
        {
            string token = "";
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = "http://1.117.93.87:8000/rbacdu/api/Login/Login";
            var response = await httpClientWrapper.PostAsync(url, model);
            return Json(response);

        }
       
        /// <summary>
        /// 注册
        /// </summary>
        /// <returns></returns>
        public IActionResult UserAdd()
        {
            return View();
        }
        /// <summary>
        ///传token  注册
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> UserAddDo(string model,string token)
        {
            
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = "http://1.117.93.87:8000/rbacxie/api/User/userCreate";
           
            string datas = JsonConvert.SerializeObject(model);
            var response = await httpClientWrapper.PostAsync(url, model);
            return Json(response);

        }

        /// <summary>
        /// 角色注册视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Role()
        {
            return View();
        }
        /// <summary>
        ///传token  添加角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> UserRoleDo(string model, string token)
        {

            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = "http://1.117.93.87:8000/rbacxie/api/Role/roleCreate";
            var response = await httpClientWrapper.PostAsync(url, model);
            return Json(response);

        }

        /// <summary>
        /// 用户角色关联表视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UserRole()
        {
            return View();
        }
        /// <summary>
        ///  用户 下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> UserListDo(string token)
        {

            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = "http://1.117.93.87:8000/rbacdu/api/User/UserSelectShow";
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);

        }

        /// <summary>
        ///  角色 下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> RoleListDo(string token)
        {

            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = "http://1.117.93.87:8000/rbacdu/api/Role/RoleSelectShow";
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);

        }



        /// <summary>
        ///用户角色添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> UserRoleAddDo(string model, string token)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = "http://1.117.93.87:8000/rbacxie/api/Role/UserRoleCreate";
            var response = await httpClientWrapper.PostAsync(url, model);
            return Json(response);

        }


        /// <summary>
        /// 菜单表
        /// </summary>
        /// <returns></returns>
        public IActionResult Menu()
        {
            return View();
        }

        // <summary>
        ///菜单显示  父级
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> MenuPidDo(string token)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = "http://1.117.93.87:8000/rbacdu/api/Menu/MenuSelectShow";
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }

        /// <summary>
        ///菜单显示  树形
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> MenuListDo( string token)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = "http://1.117.93.87:8000/rbacdu/api/Menu/MenuTreeShow";
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }

        /// <summary>
        ///菜单添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> MenuAddDo(string model, string token)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = "http://1.117.93.87:8000/rbacxie/api/Menu/menuCreate";
            var response = await httpClientWrapper.PostAsync(url, model);
            return Json(response);
        }



        /// <summary>
        /// 菜单角色表
        /// </summary>
        /// <returns></returns>
        public IActionResult MenuRole()
        {
            return View();
        }

        /// <summary>
        ///菜单显示  树形
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> MenuListTreeDo(string token)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = "http://1.117.93.87:8000/rbacdu/api/Menu/MenuTreeShowByRoleId";
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }

        /// <summary>
        ///菜单角色添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> MenuRoleAddDo(string model, string token)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = "http://1.117.93.87:8000/rbacxie/api/Menu/RoleMenu";
            var response = await httpClientWrapper.PostAsync(url, model);
            return Json(response);
        }

        /// <summary>
        ///获取用户姓名
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetUserName( string token)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = _configuration["Url"] + "ckd/api/Injure/GetUserName";
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }


        
        /// <summary>
        ///根据角色 反查用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> UserRoleListDo(int roleId, string token)
        {
            HttpClientWrapper httpClientWrapper = new HttpClientWrapper(token);
            string url = "http://1.117.93.87:8000/rbacdu/api/Role/RoleFindByUserId?id=" + 1;
            var response = await httpClientWrapper.GetAsync(url);
            return Json(response);
        }



    }
}